﻿/*
    Copyright (c) 2008 - 2009, Carlos Guzmán Álvarez

    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification, 
    are permitted provided that the following conditions are met:

        * Redistributions of source code must retain the above copyright notice, 
          this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above copyright notice, 
          this list of conditions and the following disclaimer in the documentation and/or 
          other materials provided with the distribution.
        * Neither the name of the author nor the names of its contributors may be used to endorse or 
          promote products derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using XClient.Commands;
using XClient.Configuration;
using XClient.Infrastructure;
using XClient.Views.Preferences;

namespace XClient
{
    /// <summary>
    /// Configuration manager class
    /// </summary>
    public sealed class ConfigurationManager
    {
        #region · Fields ·

        private Account                     selectedAccount;
        private XClientConfiguration        configuration;
        private PreferencesCommandsProxy    commandProxy;
        private IUnityContainer             container;

        #endregion

        #region · Properties ·

        /// <summary>
        /// Gets the account information selected for login
        /// </summary>
        public Account SelectedAccount
        {
            get { return this.selectedAccount; }
            set { this.selectedAccount = value; }
        }

        /// <summary>
        /// Gets the application configuration information
        /// </summary>
        public XClientConfiguration Configuration
        {
            get { return this.configuration; }
        }

        #endregion

        #region · Command Properties ·

        /// <summary>
        /// Gets or sets the open preferences view command
        /// </summary>
        /// <value>The add new command.</value>
        public DelegateCommand<object> OpenPreferencesViewCommand
        {
            get;
            private set;
        }

        #endregion

        #region · Constructors ·

        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigurationManager"/> class.
        /// </summary>
        public ConfigurationManager(IUnityContainer container)
        {
            this.container      = container;
            this.configuration  = XClientConfiguration.GetConfiguration();
            this.InitializeCommands();
            this.RegisterCommands();
            this.ActivateCommands();
        }

        #endregion
        
        #region · Methods ·

        /// <summary>
        /// Gets the configuration by reading it from the configuration file.
        /// </summary>
        /// <returns></returns>
        public XClientConfiguration GetConfiguration()
        {
            this.configuration = XClientConfiguration.GetConfiguration();

            return this.Configuration;
        }

        #endregion

        #region · Command Actions ·

        /// <summary>
        /// Gets a value that indicates if the view can be closed
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private bool CanOpenPreferencesView(object obj)
        {
            //return ((this.container.Resolve<XmppSession>().State == XmppSessionState.LoggedOut ||
            //        this.container.Resolve<XmppSession>().State == XmppSessionState.LoggedIn));

            return true;
        }

        /// <summary>
        /// Closes the view
        /// </summary>
        /// <param name="obj"></param>
        private void OpenPreferencesView(object obj)
        {
            IRegionManager  regionManager   = this.container.Resolve<IRegionManager>();
            IRegion         region          = regionManager.Regions[RegionNames.PreferencesRegion];
            AccountView     view            = this.container.Resolve<AccountView>();
            
            region.Add(view);
            region.Activate(view);
        }

        #endregion

        #region · Command Methods ·

        /// <summary>
        /// Initializes the commands.
        /// </summary>
        private void InitializeCommands()
        {
            this.commandProxy               = new PreferencesCommandsProxy();
            this.OpenPreferencesViewCommand = new DelegateCommand<object>(
                OpenPreferencesView,
                CanOpenPreferencesView);
        }

        /// <summary>
        /// Registers the commands.
        /// </summary>
        private void RegisterCommands()
        {
            this.commandProxy.OpenPreferencesCommand.RegisterCommand(this.OpenPreferencesViewCommand);
        }

        /// <summary>
        /// Activates the commands
        /// </summary>
        private void ActivateCommands()
        {
            this.OpenPreferencesViewCommand.IsActive = true;
        }

        #endregion
    }
}
